<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	
	String pnStr = request.getParameter("pn");
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String searchDate = request.getParameter("searchDate");
	
	int pn = 0;
	int pageSize = 10;
	Integer teamId = (Integer)session.getAttribute("tid");
	
	if(pnStr != null && !"".equals(pnStr.trim())){
		try{
			pn = Integer.parseInt(pnStr);
		}catch(Exception e){
			pn = 0;
		}
	}
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "isDelete", 0});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "rcode", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "proCode", searchKeyword});
		else if("3".equals(searchType))
			where.add(new Object[]{"like", "p.name", searchKeyword});
	}
	
	if(searchDate != null && !"".equals(searchDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(searchDate);
		where.add(new Object[]{"eq", "receiverDate", dateVal});
	}
	
	Map map = receiveorderDao.getReceiveorder(pn, pageSize,
			new String[][]{new String[]{"desc","date"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","rcode"},new String[]{"","proCode"},new String[]{"","p.name"},new String[]{"","receiverDate"}}		
			,new String[]{"project"});
	
	request.setAttribute("map",map);
	
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("searchDate",searchDate);
	
	pageContext.forward("/vm/stock/stockList.vm");
%>